<?php

namespace app\modules\admin\controllers;

use app\common\services\StaticServices;
use app\models\MemberNotice;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Response;

class NoticeController extends Controller
{
    public function actionIndex()
    {
        $p = Yii::$app->request->get('p', 1);
        $user_info = MemberNotice::find()->where(['member_id' => 0, 'notice_status' => 1]);

        $page_size = StaticServices::$page;
        $total_count = $user_info->count();
        $total_page = ceil($total_count / $page_size);

        $list = $user_info->offset(($p - 1) * $page_size)->orderBy(['update_time' => SORT_DESC])->limit($page_size)->all();

        return $this->render('index', ['list' => $list, 'p' => $p, 'total_page' => $total_page]);
    }

    public function actionSet()
    {
        $notice_id = Yii::$app->request->get('id');
        $notice_info = [];
        if ($notice_id) {
            $notice_info = MemberNotice::find()->where(['id' => $notice_id])->one();
        }

        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $notice_title = trim(Yii::$app->request->post('notice_title'));
                $notice_content = Yii::$app->request->post('notice_content');
                $notice_id = Yii::$app->request->post('notice_id');

                if (mb_strlen($notice_title, 'utf-8') < 1) {
                    throw new Exception('请填写通告标题');
                }
                if (mb_strlen($notice_content, 'utf-8') < 1) {
                    throw new Exception('请填写通告内容');
                }

                $notice_arr = MemberNotice::find()->where(['id' => $notice_id])->one();
                if ($notice_arr) {
                    $notice_info = $notice_arr;
                } else {
                    $notice_info = new MemberNotice();
                    $notice_info->create_time = time();
                }
                $notice_info->notice_title = $notice_title;
                $notice_info->notice_content = $notice_content;
                $notice_info->update_time = time();
                $notice_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }

        return $this->render('set', ['info' => $notice_info]);
    }

    public function actionOps()
    {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $notice_id = Yii::$app->request->post('notice_id');
                if (!$notice_id) {
                    throw new Exception('参数错误');
                }
                $notice_info = MemberNotice::find()->where(['id' => $notice_id, 'notice_status' => 1])->one();
                if (!$notice_info) {
                    throw new Exception('数据有误');
                }
                $notice_info->notice_status = 0;
                $notice_info->update_time = time();
                $notice_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }
    }
}
